package com.edu.test;


import com.edu.bean.*;
import com.edu.mapper.DepartMapper;
import com.edu.mapper.EmpMapper;
import com.edu.mapper.OrderMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest02 {

    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(path);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void test(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.selectEmpById(2);
        System.out.println(emp);
        System.out.println(emp.getDepart());
    }

    @Test
    public void test2(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> empList = empMapper.selectAllEmp();

        empList.get(4).getDepart();
    }

    @Test
    public void test3(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<EmpVo> empVos = empMapper.selectEmpVoBySex("女");
        for (EmpVo empVo : empVos) {
            System.out.println(empVo);
        }
    }

    @Test
    public void test04(){
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        Depart depart = departMapper.selectDepartById2(1);
        System.out.println(depart);
        for (Emp emp : depart.getEmpList()) {
            System.out.println(emp);
            System.out.println(emp.getDepart());

        }
    }


    @Test
    public void test05(){
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        List<Depart> departList = departMapper.selectAllDepart();
        for (Depart depart : departList) {
            System.out.println(depart);
            System.out.println("该部门员工如下:");
            for (Emp emp : depart.getEmpList()) {
                System.out.println(emp);
            }
            System.out.println("   ");
        }
    }

    @Test
    public void test06(){
        DepartMapper departMapper = sqlSession.getMapper(DepartMapper.class);
        List<Depart> departList = departMapper.selectDepartByPhone("12344453524");
        for (Depart depart : departList) {
            System.out.println(depart);
            for (Emp emp : depart.getEmpList()) {
                System.out.println(emp);
            }
        }

    }

}
